Open
Conversation
blake2s:
* From<[u8; 16]>
blake2b:
* From<[u8; 16]>
* From<[u8; 32]>
Both introduce an intermediate buffer in the implementation, but
this is a small tradeoff that allows usage of some common hash
sizes more idiomatically for the users of this library.
Author
|
@oconnor663 Any plans to check this out? |
Owner
|
Thanks, this looks like a good idea. What would you think about doing it generically, something like this: diff --git a/blake2b/src/lib.rs b/blake2b/src/lib.rs
index 6c4f92f..aa58385 100644
--- a/blake2b/src/lib.rs
+++ b/blake2b/src/lib.rs
@@ -616,17 +616,20 @@ fn bytes_to_hex(bytes: &[u8]) -> HexString {
s
}
-impl From<[u8; OUTBYTES]> for Hash {
- fn from(bytes: [u8; OUTBYTES]) -> Self {
+impl<const N: usize> From<[u8; N]> for Hash {
+ fn from(bytes: [u8; N]) -> Self {
+ assert!(N <= OUTBYTES, "Hash is at most {OUTBYTES} bytes");
+ let mut buf = [0u8; OUTBYTES];
+ buf[0..N].copy_from_slice(&bytes);
Self {
- bytes,
- len: OUTBYTES as u8,
+ bytes: buf,
+ len: N as u8,
}
}
}
-impl From<&[u8; OUTBYTES]> for Hash {
- fn from(bytes: &[u8; OUTBYTES]) -> Self {
+impl<const N: usize> From<&[u8; N]> for Hash {
+ fn from(bytes: &[u8; N]) -> Self {
Self::from(*bytes)
}
}
diff --git a/blake2s/src/lib.rs b/blake2s/src/lib.rs
index f845e96..c2852b4 100644
--- a/blake2s/src/lib.rs
+++ b/blake2s/src/lib.rs
@@ -608,17 +608,20 @@ fn bytes_to_hex(bytes: &[u8]) -> HexString {
s
}
-impl From<[u8; OUTBYTES]> for Hash {
- fn from(bytes: [u8; OUTBYTES]) -> Self {
+impl<const N: usize> From<[u8; N]> for Hash {
+ fn from(bytes: [u8; N]) -> Self {
+ assert!(N <= OUTBYTES, "Hash is at most {OUTBYTES} bytes");
+ let mut buf = [0u8; OUTBYTES];
+ buf[0..N].copy_from_slice(&bytes);
Self {
- bytes,
- len: OUTBYTES as u8,
+ bytes: buf,
+ len: N as u8,
}
}
}
-impl From<&[u8; OUTBYTES]> for Hash {
- fn from(bytes: &[u8; OUTBYTES]) -> Self {
+impl<const N: usize> From<&[u8; N]> for Hash {
+ fn from(bytes: &[u8; N]) -> Self {
Self::from(*bytes)
}
} |
Author
|
I mean yeah, that looks like a good solution as well. Do you want me to update the pull request this way? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
blake2s:
* From<[u8; 16]>
blake2b:
* From<[u8; 16]>
* From<[u8; 32]>
Both introduce an intermediate buffer in the implementation, but this is a small tradeoff that allows usage of some common hash sizes more idiomatically for the users of this library.